package projectsupporter.common.operation;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;

import projectsupporter.common.control.Path;
import projectsupporter.common.logic.PlugLogic;

import dbTools.DBToolEngine;
import dbTools.XML.ValidatorXMLService;
import dbTools.XML.XMLService;
import dbTools.coeModel.CodeModel;
import dbTools.factory.CodeModelFactory;
import dbTools.pojo.JavaBeanCreator;
import dbTools.tableInfo.info.TableInfo;

public class CreateMasterOperation implements IRunnableWithProgress{

	private Map<String,TableInfo> tableInfos;
	private String modelName;
	
	public CreateMasterOperation(Map<String, TableInfo> tableInfos,String modelName) {
		super();
		this.tableInfos = tableInfos;
		this.modelName=modelName;
	}



	@Override
	public void run(IProgressMonitor monitor) throws InvocationTargetException,
			InterruptedException {
		
		monitor.beginTask("Create masters ", tableInfos.size()+1);
		
		CodeModel model=CodeModelFactory.getCodeModel(modelName);
		
		model.setMainPath(DBToolEngine.getWorkSpacePath());
		
		for (Map.Entry<String, TableInfo> entry : tableInfos.entrySet()) 
		{
			monitor.subTask(entry.getValue().getTableName());
			model.writeCodes(entry.getValue());
			monitor.worked(1);
		}
		
		if(model.getMainMethod()!=null)
		{
			model.getMainMethod().doMainMethod(model,tableInfos);
		}
		monitor.worked(1);
		
		monitor.done();
	}

}
